
package com.xifan.myaccount.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xifan.myaccount.R;
import com.xifan.myaccount.entity.RecordDetail;
import com.xifan.myaccount.entity.TypeStats;
import com.xifan.myaccount.widget.MoneyView;

import java.util.List;

public class TypeStatsAdapter extends BaseAdapter {

    private List<TypeStats> list;
    private Context mContext;

    private float mDensity;

    public TypeStatsAdapter(Context context, List<TypeStats> list, int opType) {
        mContext = context;
        this.list = list;
        mDensity = context.getResources().getDisplayMetrics().density;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = View.inflate(mContext, R.layout.view_statistics_table, null);
            holder.typeName = (TextView) view.findViewById(R.id.stat_type_name);
            holder.typeMoney = (MoneyView) view.findViewById(R.id.stat_type_money);
            holder.percentageBar = (ImageView) view.findViewById(R.id.percentage_bar);
            holder.percentageText = (TextView) view.findViewById(R.id.percentage_text);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        TypeStats type = list.get(position);
        RecordDetail tmpDetail = new RecordDetail();
        tmpDetail.setMoney(type.getTypeMoney());

        holder.typeName.setText(type.getTypeName());
        holder.typeMoney.setText(tmpDetail.getMoney());

        LayoutParams params = holder.percentageBar.getLayoutParams();
        params.width = (int) (type.getPercentage() * 3 * mDensity);
        holder.percentageBar.setLayoutParams(params);
        holder.percentageText.setText(type.getPercentage() + "%");

        return view;
    }

    class ViewHolder {
        TextView typeName;
        MoneyView typeMoney;
        ImageView percentageBar;
        TextView percentageText;
    }

}
